/**
 * small-spring
 *
 * @author yuanchen
 * @date 2021-07-02 15:25
 */
package cn.bugstack.springframework.utils;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 *
 * @author yuanchen
 * @date 2021-07-02 15:25
 */
public class BeanUtil {

    public  static  void setFieldValue(Object bean,String fieldName,Object value){
        try {
            Method[] methods = bean.getClass().getMethods();

            System.out.println(value.getClass());

            Method method=null;
            //todo 暂时只对cglib代理对象处理
            if(isCglibObject(value.getClass())){
                method = bean.getClass().getMethod("set" + StringUtil.firstCharToUpperCase(fieldName),value.getClass().getSuperclass());
            }else{
                method= bean.getClass().getMethod("set" + StringUtil.firstCharToUpperCase(fieldName),value.getClass());
            }

            method.invoke(bean,value);

        }catch (Exception e){
            throw new RuntimeException(" error setFieldValue:"+fieldName);
        }

    }

    public static Boolean isCglibObject(Class beanClass){
        return beanClass.getName().contains("$$EnhancerByCGLIB$$");
    }
}
